package com.order.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.TotalClass;
import util.UtilDAO;

import com.dianbo.bean.DianboType;
import com.order.bean.TbOrder;
import com.order.bean.TbWuxiaoOrder;

public class TbOrderDao {

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-11
	 *下午04:42:40
	 *zzClub
	 *com.order.dao
	 */
	private String sql = "";
	private DianboType dianboType=null;
	private ResultSet rs = null;
	private List<TbOrder> orderList = null;
	private Statement st = null;
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据条件得到相应的指令记录
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-11
	 *下午04:47:14
	 *zzClub
	 *com.order.dao
	 *List<TbOrder>
	 */
	public List<TbOrder> getOrderList(String table,String order,String condition){
		sql=UtilDAO.getList(table, order, condition);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(orderList==null){
					orderList = new ArrayList<TbOrder>();
				}
				orderList.add(this.getOrder(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orderList;
	}
	public TbOrder getOrder(ResultSet rs){
		TbOrder tbOrder = new TbOrder();
		try {
			tbOrder.setCreateTime(rs.getString("createTime"));
			tbOrder.setOrderId(rs.getInt("orderId"));
			tbOrder.setTypeId(rs.getString("typeId"));
			tbOrder.setOrderType(rs.getInt("orderType"));
			tbOrder.setOrderValue(rs.getString("orderValue"));
			tbOrder.setUserId(rs.getInt("userId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tbOrder;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:新增一指令
	 * @param order
	 * @return
	 *2011-4-11
	 *下午10:33:02
	 *zzClub
	 *com.order.dao
	 *boolean
	 */
	public boolean saveOrder(TbOrder order){
		boolean bo = false;
		sql="insert into tb_order(userId,typeId,orderValue,createTime,orderType)" +
				" values("+order.getUserId()+",'"+order.getTypeId()+"','"+order.getOrderValue()+"','"+order.getCreateTime()+"',"+order.getOrderType()+")";
		bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:修改单个指令
	 * @param order
	 * @return
	 *2011-4-11
	 *下午10:54:31
	 *zzClub
	 *com.order.dao
	 *boolean
	 */
	public boolean modifyOrder(TbOrder order){
		boolean bo = false;
		sql="update tb_order set orderValue='"+order.getOrderValue()+"',createTime='"+order.getCreateTime()+"' where orderId="+order.getOrderId();
		bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据指令ID删除单个对象
	 * @param orderId
	 * @return
	 *2011-4-11
	 *下午10:58:44
	 *zzClub
	 *com.order.dao
	 *boolean
	 */
	public boolean deleteOrder(int orderId){
		boolean bo = false;
		sql="delete tb_order where orderId="+orderId;
		bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-12
	 *上午08:19:50
	 *zzClub
	 *com.order.dao
	 *List<TbWuxiaoOrder>
	 */
	public List<TbWuxiaoOrder> getWuxiaoList(String table,String order,String condition){
		List<TbWuxiaoOrder> wuxiaoList = null;
		sql=UtilDAO.getList(table, order, condition);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(wuxiaoList==null){
					wuxiaoList = new ArrayList<TbWuxiaoOrder>();
				}
				wuxiaoList.add(this.getWuxiao(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return wuxiaoList;
	}
	
	public TbWuxiaoOrder getWuxiao(ResultSet rs){
		TbWuxiaoOrder wuxiao = new TbWuxiaoOrder();
		try {
			wuxiao.setInvalidContent(rs.getString("invalidContent"));
			wuxiao.setInvalidId(rs.getInt("invalidId"));
			wuxiao.setUserId(rs.getInt("userId"));
			wuxiao.setStatus(rs.getInt("status"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return wuxiao;
	}
	public boolean saveWuxiao(TbWuxiaoOrder wuxiao){
		boolean bo = false;
		
		boolean boo = this.getWuxiao(wuxiao.getUserId());
		if(boo){
			sql = "update tb_invalid set invalidContent='"+wuxiao.getInvalidContent()+"',status="+wuxiao.getStatus()+" where userId="+wuxiao.getUserId();
		}else{
			sql="insert into tb_invalid (userId,invalidContent,status) values("+wuxiao.getUserId()+",'"+wuxiao.getInvalidContent()+"',"+wuxiao.getStatus()+")";
		}
		bo = new TotalClass().operatorObject(sql);
		return bo ;
	}
	public boolean getWuxiao(int userId){
		boolean bo = false;
		sql = "select count(*) cnt from tb_invalid where userId="+userId;
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				int num = rs.getInt("cnt");
				if(num>0){
					bo = true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo ;
	}
	
}
